package org.mx.test.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.mx.service.rest.GraphQLBaseResource;
import org.mx.service.rest.graphql.GraphQLFactory;
import org.mx.service.rest.vo.DataVO;
import org.mx.spring.session.SessionDataStore;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

@SuppressWarnings("javadoc")
@Path("/graphql/v1")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class GraphQLResource extends GraphQLBaseResource {
	private SessionDataStore sessionDataStore;

	@Autowired
	public GraphQLResource(GraphQLFactory factory, SessionDataStore sessionDataStore) {
		super(factory);
		this.sessionDataStore = sessionDataStore;
	}

	@Path("mutation")
	@POST
	public DataVO<JSONObject> mutation(@Context HttpHeaders headers, JSON request) {
		sessionDataStore.setCurrentUserCode("system");
		DataVO<JSONObject> result = super.mutation(request);
		sessionDataStore.removeCurrentUserCode();
		return result;
	}

	@Path("query")
	@POST
	public DataVO<JSONObject> query(@Context HttpHeaders headers, JSON request) {
		sessionDataStore.setCurrentUserCode("system");
		DataVO<JSONObject> result = super.query(request);
		sessionDataStore.removeCurrentUserCode();
		return result;
	}
}
